﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace UbbTimetable2
{
    /// <summary>
    /// A struct to store teaching data.
    /// </summary>
    public struct Teaching
    {
        public Day _dayData;

        public Day DayData
        {
            get { return _dayData; }
            set { _dayData = value; }
        }

        int _startTime;

        public int StartTime
        {
            get { return _startTime; }
            set { _startTime = value; }
        }
        int _endTime;

        public int EndTime
        {
            get { return _endTime; }
            set { _endTime = value; }
        }

        public Classroom _classroomData;

        public Classroom ClassroomData
        {
            get { return _classroomData; }
            set { _classroomData = value; }
        }

        public Subgroup _subgroupData;

        public Subgroup SubgroupData
        {
            get { return _subgroupData; }
            set { _subgroupData = value; }
        }

        public Classes _classData;

        public Classes ClassData
        {
            get { return _classData; }
            set { _classData = value; }
        }
    }

    /// <summary>
    /// A DAO that handles all data related to teachings.
    /// </summary>
    public class TeachingDao : Dao
    {
        private String err = "";

        /// <summary>
        /// The last error message.
        /// </summary>
        public String Err
        {
            get { return err; }
        }

        /// <summary>
        /// Schedules a class using the parameters.
        /// </summary>
        /// <param name="classId">The id of the class.</param>
        /// <param name="dayId">The id of the day it will occour.</param>
        /// <param name="classroomId">The id of the classroom it will take place in.</param>
        /// <param name="subgroupId">The id of the subgroup whom it will be held.</param>
        /// <param name="startTime">The starting time.</param>
        /// <param name="endTime">The ending time.</param>
        public void ScheduleClass(int classId, int dayId, int classroomId, int subgroupId, int startTime, int endTime)
        {
            String[] parameterNames = new string[6], parameterValues = new string[6];
            parameterNames[0] = "@oraId"; parameterValues[0] = classId.ToString();
            parameterNames[1] = "@napId"; parameterValues[1] = dayId.ToString();
            parameterNames[2] = "@teremId"; parameterValues[2] = classroomId.ToString();
            parameterNames[3] = "@alcsoportId"; parameterValues[3] = subgroupId.ToString();
            parameterNames[4] = "@kezdIdo"; parameterValues[4] = startTime.ToString();
            parameterNames[5] = "@vegIdo"; parameterValues[5] = endTime.ToString();
            String name = "Idozit";

            err = ExecuteStoredProcedureNonQuery(name, parameterNames, parameterValues);
        }

        /// <summary>
        /// Removes a scheduled class from the database.
        /// </summary>
        /// <param name="dayId">The id of the day it's scheduled on.</param>
        /// <param name="startTime">The starting time.</param>
        /// <param name="endTime">The end time.</param>
        /// <param name="classId">The id of the class.</param>
        public void DeleteTeaching(int dayId, int startTime, int endTime, int classId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("DELETE FROM Tanitas WHERE NapID="); sb.Append(dayId);
            sb.Append(" AND KezdIdo="); sb.Append(startTime);
            sb.Append(" AND VegIdo="); sb.Append(endTime);
            sb.Append(" AND OraID="); sb.Append(classId);

            err = ExecuteNonQuery(sb.ToString());
        }

        /// <summary>
        /// Returns the list of teachings according to the parameters.
        /// </summary>
        /// <param name="dayId">The id of the day or 0 for all.</param>
        /// <param name="groupName">The name of the group or "any" for all.</param>
        /// <param name="subgroupId">The id of the subgroup or 0 for all.</param>
        /// <param name="profId">The id of the professor or 0 for all.</param>
        /// <param name="buildingId">The id of the building or 0 for all.</param>
        /// <param name="classroomId">The id of the classroom or 0 for all.</param>
        /// <returns>The list of teachings.</returns>
        public List<Teaching> GetTeachingList(int dayId, String groupName, int subgroupId,
            int profId, int buildingId, int classroomId)
        {
            //String query = "SELECT * FROM Tanitasok";
            // 	@dayId  @groupName @subgroupId @profId  @buildingId  @classroomId 
            string[] parameterNames = new string[6], parameterValues = new string[6];
            parameterNames[0] = "@dayId"; parameterValues[0] = dayId.ToString();
            parameterNames[1] = "@groupName"; parameterValues[1] = groupName;
            parameterNames[2] = "@subgroupId"; parameterValues[2] = subgroupId.ToString();
            parameterNames[3] = "@profId"; parameterValues[3] = profId.ToString();
            parameterNames[4] = "@buildingId "; parameterValues[4] = buildingId.ToString();
            parameterNames[5] = "@classroomId "; parameterValues[5] = classroomId.ToString();

            String name = "TanitasFilter";

            //SqlDataReader rdr = ExecuteReader(query, ref err);
            SqlDataReader rdr = ExecuteStoredProcedure(name, parameterNames, parameterValues, ref err);

            List<Teaching> list = new List<Teaching>();
            if (err == "OK")
            {
                while (rdr.Read())
                {
                    Teaching item = new Teaching();
                    item.StartTime = Convert.ToInt32(rdr[0]);
                    item.EndTime = Convert.ToInt32(rdr[1]);

                    item._dayData.DayId = Convert.ToInt32(rdr[2]);
                    item._dayData.DayName = rdr[3].ToString().TrimEnd();

                    item._classroomData.CrId = Convert.ToInt32(rdr[4]);
                    item._classroomData.CrName = rdr[5].ToString().TrimEnd();

                    item._subgroupData.SgId = Convert.ToInt32(rdr[6]);
                    item._subgroupData.SgName = rdr[7].ToString().TrimEnd();

                    item._classData.Id = Convert.ToInt32(rdr[8]);
                    item._classData.TypeId = Convert.ToInt32(rdr[9]);
                    item._classData.TypeName = rdr[10].ToString().TrimEnd();
                    item._classData.ClassNameId = Convert.ToInt32(rdr[11]);
                    item._classData.ClassName = rdr[12].ToString().TrimEnd();
                    item._classData.ProfId = Convert.ToInt32(rdr[13]);
                    item._classData.ProfFullName = rdr[14].ToString().TrimEnd() +
                        " " + rdr[15].ToString().TrimEnd() + " " +
                        rdr[16].ToString().TrimEnd();

                    list.Add(item);
                }
            }
            CloseDataReader(rdr);
            return list;
        }
    }
}
